// wii_nuplay_dll.cpp : Defines the exported functions for the DLL application.
//

#include "stdafx.h"
wiimote wiimote;

/*
 * Class:     org_nuplay_NuPlayWiiMoteDevice
 * Method:    nConnect
 * Signature: (I)Z
 */
JNIEXPORT jboolean JNICALL Java_org_nuplay_NuPlayWiiMoteDevice_nConnect
(JNIEnv *, jclass, jint in_device_number) {
		bool connected = wiimote.Connect(in_device_number - 1);
		switch(in_device_number) {
			case 1:
				wiimote.SetLEDs(0x01);
				break;
			case 2:
				wiimote.SetLEDs(0x02);
				break;
			case 3:
				wiimote.SetLEDs(0x04);
				break;
			case 4:
				wiimote.SetLEDs(0x08);
				break;
		}
		
		if(wiimote.bExtension) wiimote.SetReportType(wiimote::IN_BUTTONS_ACCEL_IR_EXT); // no IR dots
	else wiimote.SetReportType(wiimote::IN_BUTTONS_ACCEL_IR);	 //    IR dots
	return connected;
}


/*
 * Class:     org_nuplay_NuPlayWiiMoteDevice
 * Method:    nGetButtonStatus
 * Signature: (I)Z
 */
JNIEXPORT jboolean JNICALL Java_org_nuplay_NuPlayWiiMoteDevice_nGetButtonStatus
(JNIEnv *, jclass, jint in_button_number) {
	if (wiimote.IsConnected()) {
		wiimote.RefreshState();
		switch(in_button_number) {
			case org_nuplay_NuPlayWiiMoteButtons_A:
				return wiimote.Button.A();
				break;
					case org_nuplay_NuPlayWiiMoteButtons_B:
				return wiimote.Button.B();
				break;
						case org_nuplay_NuPlayWiiMoteButtons_Plus:
							return wiimote.Button.Plus();
				break;
						case org_nuplay_NuPlayWiiMoteButtons_Minus:
				return wiimote.Button.Minus();
				break;
						case org_nuplay_NuPlayWiiMoteButtons_Home:
				return wiimote.Button.Home();
				break;
						case org_nuplay_NuPlayWiiMoteButtons_Up:
				return wiimote.Button.Up();
				break;
						case org_nuplay_NuPlayWiiMoteButtons_Down:
				return wiimote.Button.Down();
				break;
						case org_nuplay_NuPlayWiiMoteButtons_Left:
				return wiimote.Button.Left();
				break;
						case org_nuplay_NuPlayWiiMoteButtons_Right:
				return wiimote.Button.Right();
				break;
						case org_nuplay_NuPlayWiiMoteButtons_One:
				return wiimote.Button.One();
				break;
							case org_nuplay_NuPlayWiiMoteButtons_Two:
				return wiimote.Button.Two();
				break;
							default:
								return false;
		}
	} else {
		return false;
	}
}

/*
 * Class:     org_nuplay_NuPlayWiiMoteDevice
 * Method:    nGetPitch
 * Signature: ()F
 */
JNIEXPORT jfloat JNICALL Java_org_nuplay_NuPlayWiiMoteDevice_nGetPitch
(JNIEnv *, jclass) {
		if (wiimote.IsConnected()) {
			wiimote.RefreshState();
			return wiimote.Acceleration.Orientation.Pitch;
		} else {
			return -1;
		}
}

/*
 * Class:     org_nuplay_NuPlayWiiMoteDevice
 * Method:    nGetRoll
 * Signature: ()F
 */
JNIEXPORT jfloat JNICALL Java_org_nuplay_NuPlayWiiMoteDevice_nGetRoll
(JNIEnv *, jclass) {
		if (wiimote.IsConnected()) {
			wiimote.RefreshState();
			return wiimote.Acceleration.Orientation.Roll;
		} else {
			return -1;
		}
}
/*
 * Class:     org_nuplay_NuPlayWiiMoteDevice
 * Method:    nGetDotX
 * Signature: (I)F
 */
JNIEXPORT jfloat JNICALL Java_org_nuplay_NuPlayWiiMoteDevice_nGetDotX
(JNIEnv *, jclass, jint dot_index) {
	if (wiimote.IsConnected()) {
			wiimote.RefreshState();
		wiimote_state::ir::dot &dot = wiimote.IR.Dot[dot_index];
		if(dot.bFound) {
			return dot.X;
		} else {
			return 0;
		}
	} else {
		return -1;
	}
}

/*
 * Class:     org_nuplay_NuPlayWiiMoteDevice
 * Method:    nGetDotY
 * Signature: (I)F
 */
JNIEXPORT jfloat JNICALL Java_org_nuplay_NuPlayWiiMoteDevice_nGetDotY
(JNIEnv *, jclass, jint dot_index) {
	if (wiimote.IsConnected()) {
			wiimote.RefreshState();
		wiimote_state::ir::dot &dot = wiimote.IR.Dot[dot_index];
			if(dot.bFound) {
			return dot.Y;
		} else {
			return 0;
		}
	} else {
		return -1;
	}
}

/*
 * Class:     org_nuplay_NuPlayWiiMoteDevice
 * Method:    nGetDotSize
 * Signature: (I)F
 */
JNIEXPORT jfloat JNICALL Java_org_nuplay_NuPlayWiiMoteDevice_nGetDotSize
(JNIEnv *, jclass, jint dot_index) {
	if (wiimote.IsConnected()) {
			wiimote.RefreshState();
		wiimote_state::ir::dot &dot = wiimote.IR.Dot[dot_index];
					if(dot.bFound) {
						return dot.Size;
					} else {
			return -1;
		}
	} else {
		return -1;
	}
}

/*
 * Class:     org_nuplay_NuPlayWiiMoteDevice
 * Method:    nGetAccelX
 * Signature: ()F
 */
JNIEXPORT jfloat JNICALL Java_org_nuplay_NuPlayWiiMoteDevice_nGetAccelX
(JNIEnv *, jclass) {
		if (wiimote.IsConnected()) {
			wiimote.RefreshState();
			return wiimote.Acceleration.X;
		} else {
			return -1;
		}
}

/*
 * Class:     org_nuplay_NuPlayWiiMoteDevice
 * Method:    nGetAccelY
 * Signature: ()F
 */
JNIEXPORT jfloat JNICALL Java_org_nuplay_NuPlayWiiMoteDevice_nGetAccelY
(JNIEnv *, jclass) {
		if (wiimote.IsConnected()) {
			wiimote.RefreshState();
			return wiimote.Acceleration.Y;
		} else {
			return -1;
		}
}

/*
 * Class:     org_nuplay_NuPlayWiiMoteDevice
 * Method:    nGetAccelZ
 * Signature: ()F
 */
JNIEXPORT jfloat JNICALL Java_org_nuplay_NuPlayWiiMoteDevice_nGetAccelZ
(JNIEnv *, jclass) {
		if (wiimote.IsConnected()) {
			wiimote.RefreshState();
			return wiimote.Acceleration.Z;
		} else {
			return -1;
		}
}